package com.aggron.service.impl;

import com.aggron.bean.PageQuery;
import com.aggron.bean.PageResult;
import com.aggron.dao.SysAclMapper;
import com.aggron.entity.SysAcl;
import com.aggron.exception.ParamException;
import com.aggron.params.AclParams;
import com.aggron.service.ISysAclService;
import com.aggron.util.BeanValidator;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuhuan
 * @since 2018-06-02
 */
@Service
public class SysAclServiceImpl extends ServiceImpl<SysAclMapper, SysAcl> implements ISysAclService {

    @Autowired
    private SysAclMapper sysAclMapper;

    @Override
    public void save(AclParams aclParams) {
        BeanValidator.check(aclParams);
        if(checkExist(aclParams.getAclModuleId(),aclParams.getName(),aclParams.getId())){
            throw new ParamException("当前权限模块下存在相同名称的权限点");
        }
        SysAcl after=SysAcl.builder().name(aclParams.getName()).aclModuleId(aclParams.getAclModuleId()).status(aclParams.getStatus()).url(aclParams.getUrl()).type(aclParams.getType()).remark(aclParams.getRemark()).seq(aclParams.getSeq()).build();
        after.setCode(generateCode());
        after.setOperator("admin");//TODO
        after.setOperateIp("127.0.0.1");//TODO
        after.setOperateTime(new Date());
        sysAclMapper.insert(after);
    }

    private boolean checkExist(Integer aclModuleId,  String name, Integer id) {
        return false;//TODO
    }

    @Override
    public void update(AclParams aclParams) {
        BeanValidator.check(aclParams);
        if(checkExist(aclParams.getAclModuleId(),aclParams.getName(),aclParams.getId())){
            throw new ParamException("当前权限模块下存在相同名称的权限点");
        }
        SysAcl before=sysAclMapper.selectById(aclParams.getId());
        Preconditions.checkNotNull(before,"待更新的权限点不存在");
        SysAcl after=SysAcl.builder().id(aclParams.getId()).name(aclParams.getName()).aclModuleId(aclParams.getAclModuleId()).status(aclParams.getStatus()).url(aclParams.getUrl()).type(aclParams.getType()).remark(aclParams.getRemark()).seq(aclParams.getSeq()).build();
        after.setCode(generateCode());
        after.setOperator("admin");//TODO
        after.setOperateIp("127.0.0.1");//TODO
        after.setOperateTime(new Date());
        sysAclMapper.updateById(after);
    }

    /**
     * 分页查询
     * @param pageQuery
     * @param aclParams
     * @return
     */
    @Override
    public PageResult<Map<String, Object>> pageAcl(PageQuery pageQuery, AclParams aclParams) {
        Page page=new Page(pageQuery.getCurrent(),pageQuery.getPageSize());
        List<Map<String,Object>> userList=sysAclMapper.listAcl(page,aclParams);
        int count =sysAclMapper.countAcl(aclParams);

        PageResult<Map<String,Object>> pageResult=new PageResult(userList,count,pageQuery);
        return pageResult;
    }

    /**
     * 生成code
     * @return
     */
    public String generateCode(){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
        return simpleDateFormat.format(new Date())+"_"+(int)Math.random()*100;
    }


}
